package cn.csg.iotplatform.module.system.service.metadata;

import cn.csg.iotplatform.framework.common.exception.ErrorCode;
import cn.csg.iotplatform.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.csg.iotplatform.framework.web.core.util.WebFrameworkUtils;
import org.bouncycastle.asn1.cms.MetaData;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.csg.iotplatform.module.system.controller.admin.metadata.vo.*;
import cn.csg.iotplatform.module.system.dal.dataobject.metadata.MetaDataDO;
import cn.csg.iotplatform.framework.common.pojo.PageResult;
import cn.csg.iotplatform.framework.common.pojo.PageParam;
import cn.csg.iotplatform.framework.common.util.object.BeanUtils;

import cn.csg.iotplatform.module.system.dal.mysql.metadata.MetaDataMapper;

import static cn.csg.iotplatform.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.csg.iotplatform.module.system.enums.ErrorCodeConstants.*;

/**
 * 前端元数据存储 Service 实现类
 *
 * @author cping
 */
@Service
@Validated
public class MetaDataServiceImpl implements MetaDataService {

    @Resource
    private MetaDataMapper metaDataMapper;

    @Override
    public void createOrSaveMetaData(MetaDataSaveReqVO createReqVO) {
        // 插入
        MetaDataDO dataDO = getMetaDataByType(createReqVO.getType());
        //创建
        if (dataDO == null) {
            Long userId = WebFrameworkUtils.getLoginUserId();
            dataDO = new MetaDataDO();
            dataDO.setType(createReqVO.getType());
            dataDO.setMetadata(createReqVO.getMetadata());
            dataDO.setUserId(userId);
            metaDataMapper.insert(dataDO);
        } else {
            dataDO.setMetadata(createReqVO.getMetadata());
            this.metaDataMapper.updateById(dataDO);
        }
    }


    @Override
    public void deleteMetaDataById(String id) {
        // 校验存在
        if (metaDataMapper.selectById(id) == null) {
            throw exception(new ErrorCode("元数据存储不存在"));
        }
        // 删除
        metaDataMapper.deleteById(id);
    }

    @Override
    public void deleteMetaDataByType(String type) {
        // 校验存在
        MetaDataDO metaData =  getMetaDataById(type);
        if (metaData == null) {
            throw exception(new ErrorCode("元数据存储不存在"));
        }
        // 删除
        metaDataMapper.deleteById(metaData.getId());

    }


    @Override
    public MetaDataDO getMetaDataById(String id) {
        return metaDataMapper.selectById(id);
    }

    @Override
    public MetaDataDO getMetaDataByType(String type) {
        Long userId = WebFrameworkUtils.getLoginUserId();
        MetaDataDO metaDataDO =  this.metaDataMapper.selectOne(new LambdaQueryWrapperX<MetaDataDO>()
            .eq(MetaDataDO::getType, type)
            .eq(MetaDataDO::getUserId, userId));
        return metaDataDO;
    }

    @Override
    public PageResult<MetaDataDO> getMetaDataPage(MetaDataPageReqVO pageReqVO) {
        return metaDataMapper.selectPage(pageReqVO);
    }

}